package com.huawei.petapp2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.widget.wrapper.ToastWrapper;
import com.huawei.petapp2.constant.PushConst;
import com.huawei.petapp2.service.MemberRightService;
import com.huawei.petapp2.service.PurchasesService;
import com.huawei.petapp2.service.PushService;
import com.huawei.petapp2.utils.LoginUtil;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        // Push初始化
        PushService.init(this);
        // IAP在应用启动时的补单机制
        LoginUtil.loginCheck(MainActivity.this);
       /* Intent intent=new Intent(MainActivity.this,HomeActivity.class);
        startActivity(intent);*/


        //初始化Toast工具类
        ToastWrapper.init(this);
        //设置偏好设置
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        if (LoginUtil.isHuaweiLogin(this)) {
            PurchasesService.replenishForLaunch(this);
        }
    }

    private void initView() {
        // 顶部右侧消息
        ImageView mIvRight = findViewById(R.id.title_right);
        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 首页消息
                if (LoginUtil.loginCheck(MainActivity.this)) {
                    startActivity(new Intent(PushConst.ACTION_INNER_MESSAGE));
                }
            }
        });

        findViewById(R.id.iv_pet).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 宠物图片
                if (LoginUtil.isLogin(MainActivity.this)) {
                    // subscribe petvedio topic
                    PushService.subscribe(MainActivity.this, PushConst.TOPIC_IMAGE);
                }
                if (LoginUtil.loginCheck(MainActivity.this)) {
                    // Check 是否可以播放图片
                    if (!MemberRightService.isImageAvailable(MainActivity.this)) {
                        Toast.makeText(MainActivity.this, "请先在会员中心购买会员", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, MemberCenterActivity.class));
                        return;
                    }
                    startActivity(new Intent(MainActivity.this, PetImageActivity.class));
                }
            }
        });

        findViewById(R.id.main_user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 个人中心
                if (LoginUtil.loginCheck(MainActivity.this)) {
                    startActivity(new Intent(MainActivity.this, MineCenterActivity.class));
                }
            }
        });
    }


}